﻿/******************************************************************************
 * * setting_bluetooth_msg.c - implementation of dualsim setting functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
/*********************
 *      INCLUDES
 *********************/
#include "setting_global.h"

#if USE_LV_BLUETOOTH != 0

/*********************
 *      DEFINES
 *********************/
extern VOID audio_player_handle_bt_handset_connect_cnf(UINT8 connect);
/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/

VOID phone_bt_switch_message_handle(VOID * msg)
{
    mmi_msg_bt_switch_response_t * res_msg = (mmi_msg_bt_switch_response_t *)msg;
    printf("bt+++ phone_bt_switch_message_handle result = %d\n", res_msg->result);
    if(MMI_BT_SWITCH_ON_CNF == res_msg->header.MsgId) {
        if(1 == res_msg->result) {
            Bluetooth_Show_Object(TRUE);
            Bluetooth_Nvm_Save_Switch_State(TRUE);
        } else {
            lv_label_set_text_id(Bluetooth_Get_Opening_Label(), PHONE_TEXT_ID_OPEN_FAILED);
        }
    } else if(MMI_BT_SWITCH_OFF_CNF == res_msg->header.MsgId) {
        lv_obj_set_hidden(Bluetooth_Get_Opening_Label(), TRUE);
        Bluetooth_Nvm_Save_Switch_State(FALSE);
        bt_list_destroy();
        bt_adp_task_destroy();
    }
}

VOID phone_bt_inquiry_message_handle(VOID * msg)
{
    mmi_msg_bt_inquiry_response_t * res_msg = (mmi_msg_bt_inquiry_response_t *)msg;
    printf("bt+++ phone_bt_inquiry_message_handle complete = %d\n", res_msg->complete);
    if(1 == res_msg->complete) {
        Bluetooth_Stop_Scaning_Anim();
    } else {
        Bluetooth_Update_Scan_Label_Show();
    }
}

void phone_bt_paired_message_handle(void * msg)
{
    mmi_msg_bt_pair_response_t * res_msg = (mmi_msg_bt_pair_response_t *)msg;
    printf("bt+++ phone_bt_paired_message_handle: %d\n", res_msg->paired);
    Bluetooth_Update_Display();
}

void phone_bt_connect_message_handle(void * msg)
{
    mmi_msg_bt_connect_response_t * res_msg = (mmi_msg_bt_connect_response_t *)msg;
    printf("bt+++ phone_bt_connect_message_handle: %d %d\n", res_msg->connect, res_msg->for_audio);
    if(Bluetooth_Setting_Is_Top())
        Bluetooth_Update_Display();
    #if USE_LV_WATCH_AUDIO_PLAYER != 0
    if(res_msg->for_audio)
        audio_player_handle_bt_handset_connect_cnf(res_msg->connect);
    #endif
}

void phone_bt_acl_connect_message_handle(void * msg)
{
    mmi_msg_bt_acl_response_t * res_msg = (mmi_msg_bt_acl_response_t *)msg;
    printf("bt+++ phone_bt_acl_connect_message_handle %d\n", res_msg->connect);
    if(res_msg->connect == 0 && res_msg->reason == BT_ERR_CODE_CONNECTION_TERMINATED_BY_LOCAL_HOST
            || res_msg->reason == BT_ERR_CODE_REMOTE_USER_TERMINATED_CONNECTION) {
        if(Bluetooth_Setting_Is_Top())
            Bluetooth_Update_Display();
    } else {
        Bluetooth_Processing_Mbox_Update();
    }
}

void phone_bt_obex_message_handle(void * msg)
{
    mmi_msg_bt_obex_t * info = (mmi_msg_bt_obex_t *)msg;
    switch(info->header.MsgId) {
        case MMI_BT_OBEX_FILE_RECEIVE_START:
            Bluetooth_Receive_File_Confirm();
            break;
        case MMI_BT_OBEX_FILE_RECEIVING:
            Bluetooth_Transfer_File_Process_Update(PROCESS_RECV_FILE);
            break;
        case MMI_BT_OBEX_FILE_RECEIVE_FINISH:
            Bluetooth_Transfer_File_Finish(PROCESS_RECV_FILE, 1);
            break;
        case MMI_BT_OBEX_FILE_RECEIVE_ABORT:
            if(BT_RECEIVE_NO_SDCARD == info->data || BT_RECEIVE_NO_SPACE == info->data)
                Bluetooth_Receive_File_Abort_Tip(info->data);
            else
                Bluetooth_Transfer_File_Finish(PROCESS_RECV_FILE, 0);
            break;
        case MMI_BT_OBEX_FILE_SEND_FAILED:
            Bluetooth_Transfer_File_Finish(PROCESS_SEND_FILE, 0);
            break;
        case MMI_BT_OBEX_FILE_SEND_SUCCESS:
            Bluetooth_Transfer_File_Finish(PROCESS_SEND_FILE, 1);
            break;
            #if 0
        case MMI_BT_OBEX_FILE_SEND_START:
            bluetooth_send_file_start();
            break;
        case MMI_BT_OBEX_FILE_SEND_CONTINUE:
            bluetooth_transfer_file_process_update(PROCESS_SEND_FILE);
            break;
            #endif
        default:
            break;
    }
}
#endif /*USE_LV_BLUETOOTH*/
